package com.yzc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzc.model.entity.order.TOrders;
import com.yzc.model.query.order.TOrdersReq;
import com.yzc.model.vo.order.MyOrdersVo;
import com.yzc.model.vo.order.RushOrdersVo;
import com.yzc.model.vo.order.TOrdersVo;
import com.yzc.service.TOrdersService;
import com.yzc.mapper.TOrdersMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Yzc
 * @description 针对表【t_orders】的数据库操作Service实现
 * @createDate 2024-11-25 19:49:09
 */
@Service
public class TOrdersServiceImpl extends ServiceImpl<TOrdersMapper, TOrders>
        implements TOrdersService {

    @Autowired
    private TOrdersMapper tOrdersMapper;

    @Override
    public List<TOrdersVo> loadAllOrderList() {
        return tOrdersMapper.loadAllOrderList();
    }

    @Override
    public List<MyOrdersVo> loadMyOrdersList(Integer id) {
        QueryWrapper<TOrders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("worker_id", id);
        List<TOrders> ordersList = tOrdersMapper.selectList(queryWrapper);

        // 将查询结果转换为MyOrdersVo对象的列表
        return ordersList.stream()
                .map(order -> {
                    MyOrdersVo myOrdersVo = new MyOrdersVo();
                    BeanUtils.copyProperties(order, myOrdersVo);
                    return myOrdersVo;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<RushOrdersVo> rushOrdersList() {
        return tOrdersMapper.rushOrdersList();
    }

    @Override
    public void accomplishOrder(Integer id) {
        tOrdersMapper.accomplishOrder(id);
    }

    @Override
    public void rushOrderByVersion(Integer id, Integer userId) {
        // 查询最新的订单信息，包括版本号
        TOrders orders = tOrdersMapper.selectById(id);
        if (orders == null) {
            // 订单不存在，抛出异常或返回错误信息
            throw new RuntimeException("订单不存在");
        }
        if (orders.getIsDeleted() == 1) {
            // 订单已删除，抛出异常或返回错误信息
            throw new RuntimeException("订单已删除");
        }
        if (orders.getType() != 1) {
            // 订单类型不是抢单类型，抛出异常或返回错误信息
            throw new RuntimeException("订单类型不是抢单类型");
        }
        if (orders.getVersion() != 0) {
            // 订单已经被抢单，抛出异常或返回错误信息
            throw new RuntimeException("订单已经被抢单");
        }

        // 设置新的订单状态为抢单成功状态，假设2是抢单成功状态
        orders.setStatus(2);
        // 设置抢单成功的工人 ID
        orders.setWorkerId(userId);
        // 设置抢单时间
        orders.setRushTime(new Date()); // 当前时间作为抢单时间

        // 更新订单信息，乐观锁会自动处理版本号，确保数据一致性
        int updatedRows = tOrdersMapper.updateById(orders);
        if (updatedRows == 0) {
            // 更新失败，可能是版本冲突或其他原因，抛出异常或返回错误信息
            throw new RuntimeException("抢单失败，可能是订单已被其他人抢先或不存在");
        }
    }

    @Override
    public void saveOrderInfo(TOrdersReq tOrdersReq) {
        tOrdersMapper.saveOrderInfo(tOrdersReq);
    }


}




